<?php

/**
 * This is the model class for table "{{stockitems}}".
 *
 * The followings are the available columns in table '{{stockitems}}':
 * @property string $idstockitems
 * @property string $stockitemsname
 * @property string $stockitemsbrand
 * @property string $idmeasure
 * @property string $idstockgroup
 * @property string $stockitemsquantity
 * @property string $stockitemsrateper
 * @property string $stockitemsvalue
 *
 * The followings are the available model relations:
 * @property Dispatchdetails[] $dispatchdetails
 * @property Processingrate[] $processingrates
 * @property Receivedetails[] $receivedetails
 * @property Ssctest[] $ssctests
 * @property Measure $idmeasure0
 * @property Stockgroup $idstockgroup0
 * @property Stocktransferdestination[] $stocktransferdestinations
 * @property Stocktransfersource[] $stocktransfersources
 * @property Transactionsalespurchase[] $transactionsalespurchases
 */
class Stockitems extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Stockitems the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{stockitems}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('stockitemsname, stockitemsbrand', 'required'),
			array('stockitemsname, stockitemsbrand', 'length', 'max'=>45),
			array('idmeasure, idstockgroup, stockitemsquantity, stockitemsrateper, stockitemsvalue', 'length', 'max'=>10),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('idstockitems, stockitemsname, stockitemsbrand, idmeasure, idstockgroup, stockitemsquantity, stockitemsrateper, stockitemsvalue', 'safe', 'on'=>'search'),
                        array('unitsymbol', 'autocompleteValidator', 'model'=>'Measure'),
                        array('stockgroupname', 'autocompleteValidator', 'model'=>'Stockgroup'),
		);
	}        
        
        public function autocompleteValidator($attribute,$params){
               $match = addcslashes($_REQUEST[$attribute], '%_');
               if(!new $params['model']->exists(new CDbCriteria( array(
                    'condition' => "$attribute = :$attribute",
                    'params'    => array(":$attribute" => "$match")
                )))){
                    $this->addError($attribute, Yii::t('application', 'Incorrect {attribute}.', array('{attribute}'=>Yii::t('application', $attribute))));
               }
        }      

        public function beforeSave(){
                        if(isset($_REQUEST['unitsymbol'])){
                $match = addcslashes($_REQUEST['unitsymbol'], '%_'); // escape LIKE's special characters
                $criteria=new CDbCriteria( array(
                    'condition' => 'unitsymbol = :unitsymbol',
                    'params'    => array(':unitsymbol' => "$match")
                ) );            
                $model=Measure::model()->find($criteria);
                $this->idmeasure = $model->idmeasure;
            }
                        if(isset($_REQUEST['stockgroupname'])){
                $match = addcslashes($_REQUEST['stockgroupname'], '%_'); // escape LIKE's special characters
                $criteria=new CDbCriteria( array(
                    'condition' => 'stockgroupname = :stockgroupname',
                    'params'    => array(':stockgroupname' => "$match")
                ) );            
                $model=Stockgroup::model()->find($criteria);
                $this->idstockgroup = $model->idstockgroup;
            }
                        return parent::beforeSave();
        }        
        
       
        
        
	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'dispatchdetails' => array(self::HAS_MANY, 'Dispatchdetails', 'idstockitems'),
			'processingrates' => array(self::HAS_MANY, 'Processingrate', 'idstockitems'),
			'receivedetails' => array(self::HAS_MANY, 'Receivedetails', 'idstockitems'),
			'ssctests' => array(self::HAS_MANY, 'Ssctest', 'idstockitems'),
			'idmeasure0' => array(self::BELONGS_TO, 'Measure', 'idmeasure'),
			'idstockgroup0' => array(self::BELONGS_TO, 'Stockgroup', 'idstockgroup'),
			'stocktransferdestinations' => array(self::HAS_MANY, 'Stocktransferdestination', 'idstockitems'),
			'stocktransfersources' => array(self::HAS_MANY, 'Stocktransfersource', 'idstockitems'),
			'transactionsalespurchases' => array(self::HAS_MANY, 'Transactionsalespurchase', 'idstockitems'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'idstockitems' => Yii::t('application', 'Idstockitems'),
			'stockitemsname' => Yii::t('application', 'Stockitemsname'),
			'stockitemsbrand' => Yii::t('application', 'Stockitemsbrand'),
			'idmeasure' => Yii::t('application', 'Idmeasure'),
			'idstockgroup' => Yii::t('application', 'Idstockgroup'),
			'stockitemsquantity' => Yii::t('application', 'Stockitemsquantity'),
			'stockitemsrateper' => Yii::t('application', 'Stockitemsrateper'),
			'stockitemsvalue' => Yii::t('application', 'Stockitemsvalue'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('idstockitems',$this->idstockitems,true);
		$criteria->compare('stockitemsname',$this->stockitemsname,true);
		$criteria->compare('stockitemsbrand',$this->stockitemsbrand,true);
		$criteria->compare('idmeasure',$this->idmeasure,true);
		$criteria->compare('idstockgroup',$this->idstockgroup,true);
		$criteria->compare('stockitemsquantity',$this->stockitemsquantity,true);
		$criteria->compare('stockitemsrateper',$this->stockitemsrateper,true);
		$criteria->compare('stockitemsvalue',$this->stockitemsvalue,true);

		return new CActiveDataProvider(get_class($this), array(
			'criteria'=>$criteria,
		));
	}
}